//
//  OneTableViewCell.m
//  fp
//
//  Created by xingshuai on 2020/11/10.
//

#import "OneTableViewCell.h"

@interface OneTableViewCell ()

@property (nonatomic, strong) UILabel *topTitle;    // 标题
@property (nonatomic, strong) UIView *btnTypeView;  // 按钮区域
@property (nonatomic, strong) UIButton *temp_btn;   // 选中按钮转换
@property (nonatomic, strong) NSArray *arrList;     // 按钮文字

@end

@implementation OneTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView  {
    static NSString *identifier = @"XAFeedTypeCellID";
    id cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self creatViewUI];
    }
    return self;
}

-(NSArray *)arrList {
    if (!_arrList) {
        _arrList = @[@"播放相关",@"会员问题",@"界面异常",@"网络问题",@"黑屏/白屏",@"产品建议",@"影视内容",@"闪退",@"投屏问题",@"卡顿",@"其他"];
    }
    return _arrList;;
}

-(void)creatViewUI {
    self.topTitle = [UILabel new];
    self.topTitle.text = @"请选择反馈类型";
    self.topTitle.textColor = [UIColor colorWithHexString:@"#3B3B3B"];
    self.topTitle.font = [UIFont pingFangSCWithWeight:FontWeightStyleSemibold size:20];
    [self.contentView addSubview:self.topTitle];
    [self.topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.top.equalTo(self.contentView).offset(30);
        make.height.mas_equalTo(28);
    }];
    
    self.btnTypeView = [UIView new];
    [self.contentView addSubview:self.btnTypeView];
    [self.btnTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topTitle.mas_left).offset(0);
        make.top.equalTo(self.topTitle.mas_bottom).offset(24);
        make.right.equalTo(self.contentView).offset(-25);
        make.bottom.equalTo(self.contentView).offset(-15);
    }];
    
    CGFloat btn_w = (ScreenWidth*0.7-50)/3;
    CGFloat btn_h = 30;
    
    for (int i = 0; i < self.arrList.count; i ++) {
        
        CGFloat btn_x = btn_w * (i % 3);
        CGFloat btn_y = 30;
        
        if (i > 8) {
            btn_y = btn_h * 3 + 90;
        } else if (i > 5) {
            btn_y = btn_h * 2 + 60;
        } else if (i > 2) {
            btn_y = btn_h + 30;
        } else {
            btn_y = 0;
        }
                
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:self.arrList[i] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"user_feed_n"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"user_feed_s"] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor colorWithHexString:@"#3B3B3B"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.frame = CGRectMake(btn_x, btn_y, btn_w, btn_h);
        button.tag = i;
        //button.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnTypeView addSubview:button];
    }
}

-(void)clickBtn:(UIButton *)sender {
    // 防止第一次重复点击默认选中的
    if (_temp_btn == sender)
    {
        return;
    }
    else
    {
        _temp_btn.selected = NO;
        if (_temp_btn == sender)
        {
            return;
        }
        _temp_btn = sender;
        _temp_btn.selected = !_temp_btn.selected;
        
        if (_temp_btn.selected)
        {
            if (self.didSelectFeedBtnTypeBlock) {
                self.didSelectFeedBtnTypeBlock(self.arrList[sender.tag]);
            }
        }
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
